<template>
  <div>
    <el-form :model="params" ref="ruleForm" label-position="right" size="small" label-width="110px" class="base-search-more">
       <el-row>
        <el-col :span="6">
          <el-form-item label="注册电话">
            <el-input style="width: 100%" placeholder="请输入用户注册手机号" v-model="params.userMobile" clearable />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="下单状态">
            <el-select style="width: 100%" v-model="params.orderStatus" placeholder="用户下单状态" clearable>
              <el-option label="注册未下单" value="1"  />
              <el-option label="超过30天未下单" value="2" />
              <el-option label="下过单的用户" value="3" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="登录状态">
            <el-select style="width: 100%" v-model="params.loadStatus" placeholder="用户账号状态" clearable>
              <el-option label="最近有登录" value="1" />
              <el-option label="超过30天未登录" value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="用户余额">
            <el-select style="width: 100%" v-model="params.balanceUser" placeholder="用户账号状态" clearable>
              <el-option label="全部" value="" />
              <el-option label="有余额用户" value="1" />
              <el-option label="无余额用户" value="2" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item  style="text-align: right">
            <el-button type="primary" icon="el-icon-search" @click=" handleSizeChange(15)">查询</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="base-table-wrap" >
      <el-table :data="tableData.list" stripe style="width: 100%">
        <el-table-column prop="mobile" label="注册电话" min-width="100px" align="center" />
        <el-table-column prop="createtime" label="注册时间" min-width="120px" align="center" />
        <el-table-column prop="score" label="用户积分" min-width="100px" align="center" />
        <el-table-column prop="lastLoginTime" label="最后登录" min-width="120px" align="center" />
        <el-table-column prop="lastOrderTime" label="最后下单时间" min-width="120px" align="center" />
        <el-table-column prop="remainder" label="可用余额(元)" min-width="100px" align="center" />
        <el-table-column fixed="right" label="操作" width="250px" align="center">
          <template slot-scope="scope">
            <el-button class="el-text-warning" type="text" @click="setUserInterd(scope.row.id)" size="mini">设置积分</el-button>
            <el-button class="el-text-warning" type="text" @click="editPassword(scope.row.id)" size="mini">修改密码</el-button>
             <router-link style="margin-left: 5px;" :to="{path:'/main/user/message',query:{id:scope.row.id}}">
              <el-button class="el-text-check" type="text" size="mini">查看详情</el-button>
            </router-link>
          </template>
        </el-table-column>

      </el-table>
      <!-- 复制 el-table v-loading  v-loading无法快速响应 所有自定义 -->
      <div class="base-directives-loading" v-show="isloadingTable">
        <div class="el-loading-spinner">
          <svg viewBox="25 25 50 50" class="circular">
            <circle cx="50" cy="50" r="20" fill="none" class="path"></circle>
          </svg>
        </div>
      </div>
      <div class="base-pagination clearfix">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="newPage" :pageSize='pageSize'
          :page-sizes="[15, 30,45]" background layout="total,sizes, prev, pager, next, jumper"
          :total="tableData.total" />
      </div>
    </div>
    <el-dialog title="修改密码" v-dialogDrag :visible.sync="dialogTableVisible" width="380px" :close-on-press-escap="true">
      <el-form :model="ruleForm" status-icon ref="ruleForm" label-width="85px">
        <el-form-item label="新密码" prop="password" :rules="[
          {min: 6, max: 18, message: '密码6到18个字符', trigger: 'blur'},
          {validator: validatePass1, trigger: 'blur'}]">
          <el-input v-model="ruleForm.password" type="password" style="width:220px;" />
        </el-form-item>
        <el-form-item label="确认新密码" prop="password1" :rules="[
          {validator: validatePass2, trigger: 'blur'},
          {min: 6, max: 18, message: '密码6到18个字符', trigger: 'blur'}]">
          <el-input v-model="ruleForm.password1" type="password" style="width:220px;" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm')" :loading='isloadSubmit'>保存</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import imd5 from '@/utils/md5.js'
  import { modifyScore } from '@/api/user.js'
  export default {
    data() {
      return {
        params: { // 表格查询
          page: 0,
          size: 15,
          balanceUser: '',
          userMobile:'',
          orderStatus:'',
          loadStatus: '',
        },
        dialogTableVisible: false,
        isloadingTable: false, // 表单等待
        isloadSubmit: false, // 表单等待
        ruleForm: {
          id:'',
          password: '',
          password1: '',
        },
        tableData: {}, // 表单数据
        newPage: 1,
        pageSize: 15,
      }
    },
    methods: {
      validatePass1(rule, value, callback) {
        if (value === '') {
          callback(new Error('请输入密码'));
        } else {
          if (this.ruleForm.password1 !== '') {
            this.$refs.ruleForm.validateField('password1');
          }
          callback();
        }
      },
      validatePass2(rule, value, callback) {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.password) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      },
      editPassword(id) { // 打开修改密码
        this.dialogTableVisible = true
        this.$nextTick(() => {
          this.$refs['ruleForm'].resetFields()
          this.ruleForm = Object.assign({}, this.$options.data().ruleForm)
        })
        this.ruleForm.id = id
      },
      submitForm(formName) { // 修改密码
        this.$refs[formName].validate((valid) => {
          if (valid) {
            const params = { id:this.ruleForm.id, password: imd5.md5(this.ruleForm.password1)}
            this.isloadSubmit = true
            this.$http.get('/server/cuser/setPassword?'+this.qs.stringify(params)).then(res => {
              if (res.data.success) {
                this.$message.success('密码修改成功')
                this.dialogTableVisible = false
              }
            }).finally(() => { this.isloadSubmit = false })
          } else {
            return false
          }
        })
      },
      setUserInterd(id) { // 修改用户积分
        this.$prompt('请输入用户积分', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPattern: /^[1-9]([0-9])*$/,
          inputErrorMessage: '请输入整数'
        }).then(({ value }) => {
          modifyScore({ id, score: value }).then(res => {
            if (res.success) {
              this.$message.success('修改成功！')
              this.getTableList()
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          });       
        });
      },
      getTableList() { // 获取表单数据
        this.isloadingTable = true
        const params = { ...this.params }
        this.$http.get('/server/cuser/list?' + this.qs.stringify(params)).then(res => {
          if (res.data.success) {
            this.tableData = res.data.data
          }
        }).finally(() => { this.isloadingTable = false })
      },
      handleSizeChange(size) { // 设置分页数量
        this.params.size = size
        this.pageSize = size
        this.newPage = 1
        this.params.page = 0
        this.getTableList()
      },
      handleCurrentChange(page) { // 切换分页
        this.params.page = page - 1
        this.getTableList()
      }
    },
    mounted() {
      this.getTableList()
    }
  }
</script>
<style scoped>
  .checkbox-wrap {
    flex-flow: row wrap;
  }
  .checkbox-wrap .el-checkbox {
    width: 200px;
    height: 40px;
    line-height: 40px;
    margin: 0;
  }
</style>
